<template>
  <view>
    <view class="login-box" style="width: 100%;margin-top: 200rpx;">
      <view class="icon">
        <uni-icons type="contact-filled" size="90"></uni-icons>
      </view>
      <view>
        <view class="button" @click="getUserInfo">一键登录</view>
      </view>
      <view style="color: #999;font-size: 12px;margin-top: 20rpx;">
        登陆后尽享更多权益
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-login",
    data() {
      return {

      };
    },
    methods: {
      getUserInfo(e) {
        uni.getUserProfile({
          lang: 'zh_CN',
          desc: '用来授权登录该小程序!',
          success: (userInfo) => {
            this.$store.commit('setToken', userInfo)
            const vm = this;
            uni.login({
              provider: 'weixin',
              success: function(loginRes) {
                vm.getMyToken({
                  'code': loginRes.code,
                  'encryptedData': userInfo.encryptedData,
                  'iv': userInfo.iv,
                  'rawData': userInfo.rawData,
                  'signature': userInfo.signature
                })
                uni.$showMsg('登录成功!')
                if (vm.$store.state.backInfo) {
                  uni.switchTab({
                    url: vm.$store.state.backInfo
                  })
                  vm.$store.commit('clearBackInfo')
                }
              }
            })
          },
          fail: () => {
            uni.$showMsg('登录失败!')
          },
          complete: () => {},
        })
      },
      async getMyToken(data) {
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/users/wxlogin', data)
        console.log(res)
      }
    }
  }
</script>

<style lang="scss">
  .login-box {
    text-align: center;

    .button {
      margin-left: 80rpx;
      width: 75%;
      height: 100rpx;
      background-color: #c00000;
      position: relative;
      color: white;
      text-align: center;
      line-height: 100rpx;

      &::before {
        content: ' ';
        display: block;
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
        position: absolute;
        background-color: #c00000;
        top: 0;
        left: -50rpx;
      }

      &::after {
        content: ' ';
        display: block;
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
        position: absolute;
        background-color: #c00000;
        top: 0;
        right: -50rpx;
      }
    }
  }
</style>